package com.itheima.reggie.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.Constant;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Employee;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    // 分类列表
    @Override
    public List<Category> findAll() {
        return categoryMapper.findAll();
    }

    @Autowired
    private HttpSession session;

    // 新增分类
    @Override
    public void save(Category category) {
        // 1.补全信息
        // 1-1 雪花ID
        long categoryId = IdUtil.createSnowflake(1, 1).nextId();
        category.setId(categoryId);
       /* // 1-2 创建、修改时间
        category.setCreateTime(new Date());
        category.setUpdateTime(new Date());
        // 1-3 创建、修改人
        Employee currentEmp = (Employee) session.getAttribute(Constant.SESSION_EMPLOYEE);
        category.setCreateUser(currentEmp.getId());
        category.setUpdateUser(currentEmp.getId());*/

        // 2.调用mapper新增
        categoryMapper.save(category);

    }

    // 修改分类
    @Override
    public void update(Category category) {
        // 1.补全信息
      /*  // 1-1 修改时间
        category.setUpdateTime(new Date());
        // 1-2 修改人
        Employee currentEmp = (Employee) session.getAttribute(Constant.SESSION_EMPLOYEE);
        category.setUpdateUser(currentEmp.getId());*/

        // 2.调用mapper修改
        // categoryMapper.update(category);
        categoryMapper.updateById(category);
    }

    // 删除分类
    @Override
    public void deleteById(Long id) {
        // 1.先查询该分类是否有菜品
        Integer countDish = categoryMapper.countDishByCategoryId(id);
        if (countDish>0) {
            // return ResultInfo.error("该分类下有菜品，无法删除");
            throw new CustomException("该分类下有菜品，无法删除");
        }
        // 2.再查询该分类是否有套餐
        Integer countSetmeal = categoryMapper.countSetmealByCategoryId(id);
        if (countSetmeal>0) {
            // return ResultInfo.error("该分类下有套餐，无法删除");
            throw new CustomException("该分类下有套餐，无法删除");
        }
        // 3.直接删除
        categoryMapper.deleteById(id);
    }

    // 根据type查询分类列表
    @Override
    public List<Category> list(Integer type) {
        // 1.构建条件  select * from category where type=1
        LambdaQueryWrapper<Category> qw = new LambdaQueryWrapper<>();
        qw.eq(Category::getType,type);
        // 2.查询
        return categoryMapper.selectList(qw);
    }
}
